﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ershou.BLL;
using System.IO;
public partial class spck : System.Web.UI.Page
{
    public sp sp1 = new sp();
    public yh yh1 = new yh();
    protected void Page_Load(object sender, EventArgs e)
    {
        Label16.Text = "";

        string x = sp1.select1(int.Parse(Request.QueryString["ID"]));
        Image1.ImageUrl = x;
        
        Label1.Text= sp1.select7(int.Parse(Request.QueryString["ID"]));
        
        Label11.Text = sp1.select3(int.Parse(Request.QueryString["ID"]));
        Label12.Text = sp1.select4(int.Parse(Request.QueryString["ID"])) + "元";
        Label13.Text = sp1.select5(int.Parse(Request.QueryString["ID"])) + "元";
        Label14.Text = sp1.select6(int.Parse(Request.QueryString["ID"]));
        TextBox3.Text = sp1.select18(int.Parse(Request.QueryString["ID"]));
        DateTime t = sp1.select8(int.Parse(Request.QueryString["ID"]));
        DateTime nowdate = DateTime.Now;
        if (nowdate < t)
        {
            Label2.Text = "距开始拍卖还剩：";
            Button3.Text = "报名";
        }

    }

    public string nowtime()
    {
        DateTime t = sp1.select8(int.Parse(Request.QueryString["ID"]));
        DateTime nowdate = DateTime.Now;
        string a = t.Year.ToString();
        string b = t.Month.ToString();
        string c = t.Day.ToString();
        string d = t.Hour.ToString();
        string e = t.Minute.ToString();
        string f = t.Second.ToString();
        if (Convert.ToInt32(b) < 10)
        {
            b = "0" + b;
        }
        if (Convert.ToInt32(c) < 10)
        {
            c = "0" + c;
        }
        if (Convert.ToInt32(d) < 10)
        {
            d = "0" + d;
        }
        if (Convert.ToInt32(e) < 10)
        {
            e = "0" + e;
        }
        if (Convert.ToInt32(f) < 10)
        {
            f = "0" + f;
        }
        return a + "-" + b + "-" + c + "T" + d + ":" + e + ":" + f;
    }

    public string getmoney()
    {
        string money1 = sp1.select2(int.Parse(Request.QueryString["ID"]));
        string name = sp1.selectmj(int.Parse(Request.QueryString["ID"]));
        return money1+"元("+name+")";
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Session["CustomerId"] == null)
        {
            Response.Redirect("~/dl.aspx");
        }
        else
        {
            if(sp1.select11(int.Parse(Request.QueryString["ID"]), Convert.ToInt32(Session["CustomerId"])))
            {
                Label16.Text = "卖家不能参加自己的商品拍卖！";
            }
            else
            {
                if (sp1.select8(int.Parse(Request.QueryString["ID"])) > DateTime.Now || (sp1.select8(int.Parse(Request.QueryString["ID"])) - DateTime.Now).TotalSeconds * (-1) <= 24 * 60 * 60)
                {
                    if (sp1.select8(int.Parse(Request.QueryString["ID"])) < DateTime.Now && (sp1.select8(int.Parse(Request.QueryString["ID"])) - DateTime.Now).TotalSeconds <= 24 * 60 * 60)
                    {
                        int yh_id = Convert.ToInt32(Session["CustomerId"]);
                        int sp_id = int.Parse(Request.QueryString["ID"]);
                        if (!sp1.selectpm(Convert.ToInt32(yh_id), sp_id))
                        {

                            if (TextBox2.Text == "")
                            {
                                Label16.Text = "请先输入出价金额！";
                            }
                            else
                            {
                                if ((Convert.ToInt32(TextBox2.Text) - Convert.ToInt32(sp1.select4(int.Parse(Request.QueryString["ID"])))) % Convert.ToInt32(sp1.select5(int.Parse(Request.QueryString["ID"]))) == 0)
                                {
                                    if (Convert.ToInt32(TextBox2.Text) > Convert.ToInt32(sp1.select2(int.Parse(Request.QueryString["ID"]))))
                                    {
                                        sp1.Update1(int.Parse(Request.QueryString["ID"]), Convert.ToInt32(TextBox2.Text), Convert.ToInt32(Session["CustomerId"]));
                                    }
                                    else
                                    {
                                        Label16.Text = "出价金额不能低于当前拍卖价！";
                                    }
                                }
                                else
                                {
                                    Label16.Text = "出价金额不符合出价规则！";
                                }
                            }
                        }
                        else
                        {
                            Label16.Text = "您未报名本次拍卖，无法出价！";
                        }
                    }
                    else
                    {
                        int yh_id = Convert.ToInt32(Session["CustomerId"]);
                        int sp_id = int.Parse(Request.QueryString["ID"]);
                        if (sp1.selectpm(Convert.ToInt32(yh_id), sp_id)){ 
                            if (Button3.Text == "报名")
                            {
                                Response.Redirect("~/qiehuan.aspx?ID=" + Request.QueryString["ID"]);
                            }
                        }
                        else
                        {
                            Label16.Text = "拍卖未开始！";
                        }
                    }
                }
                else
                {
                    Label16.Text = "拍卖时间已结束，无法进行出价！";
                }
            }
        }
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text == "")
        {
            TextBox2.Text = (int.Parse(sp1.select2(int.Parse(Request.QueryString["ID"]))) + int.Parse(sp1.select5(int.Parse(Request.QueryString["ID"])))).ToString();
        }
        else
        {
            TextBox2.Text = (int.Parse(TextBox2.Text) + int.Parse(sp1.select5(int.Parse(Request.QueryString["ID"])))).ToString();
        }
    }
}